package com.example.springbott.service;

import com.example.springbott.bean.MarketBrand;
import com.example.springbott.bean.MarketBrandExample;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.mapper.MarketBrandMapper;
import com.example.springbott.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 品牌制造商
 *
 * @author silic xia
 * @since 2022/11/21 14:24
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService {

    @Autowired
    MarketBrandMapper brandMapper;

    /**
     * 展示list
     *
     * @param /page=1&limit=20&id=1046023&name=%E8%82%AF&sort=add_time&order=desc
     * @return 0成功 402参数不对
     * @author silic xia
     * @since 2022/11/21 14:29
     */
    @Override
    public CommonData<MarketBrand> adminBrandList(BasePageInfo info, Integer id, String name) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketBrandExample example = new MarketBrandExample();
        // 排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketBrandExample.Criteria criteria = example.createCriteria();
        // 判空
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        // 没有被删除的数据
        criteria.andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);
        return CommonData.data(pageInfo);
    }

    /**
     * 品牌制造商/新建
     *
     * @param {"name":"000000八嘎耶鲁品牌商","desc":"快一点","floorPrice":"500", "picUrl":"http://182.92.235.201:8083/wx/storage/fetch/n50pm7y8p1r39d2bn21k.JPG"}
     * @return
     * @author silic xia
     * @since 2022/11/21 15:21
     */
    @Override
    public MarketBrand adminBrandCreate(MarketBrand marketBrand) {
        marketBrand.setAddTime(new Date());
        brandMapper.insertSelective(marketBrand);
        Integer id = marketBrand.getId();
        MarketBrand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    /**
     *  品牌制造商/修改
     *
     * @param {"id":1046025,"name":"x","desc":"1111","picUrl":"","sortOrder":50,"floorPrice":1000,
     * "addTime":"2022-11-21 15:25:17","updateTime":"2022-11-21 15:25:17","deleted":false}
     * @return data:MarketBrand、errmsg: "成功" 、errno: 0
     * @author silic xia
     * @since 2022/11/21 15:22
     */
    @Override
    public MarketBrand adminBrandUpdate(MarketBrand marketBrand) {
        marketBrand.setUpdateTime(new Date());
        Integer id = marketBrand.getId();

        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        brandMapper.updateByExampleSelective(marketBrand,example);
        MarketBrand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    /**
     * 品牌制造商/删除
     * @param {"id":1046025,"name":"x","desc":"1111","picUrl":"","sortOrder":50,"floorPrice":1000,"
     * addTime":"2022-11-21 15:25:17","updateTime":"2022-11-21 15:25:17","deleted":false}
     * @return {"errno":0,"errmsg":"成功"}
     * @author silic xia
     * @since 2022/11/21 15:23
     */
    @Override
    public void adminBrandDelete(MarketBrand marketBrand) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketBrand.getId());
        marketBrand.setDeleted(true);
        marketBrand.setUpdateTime(new Date());
        brandMapper.updateByExampleSelective(marketBrand,example);
    }


}

